package com.ziyun.erp.web.modules.business.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.ziyun.erp.common.utils.UserUtils;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.business.entity.OrderScholarshipConfigEntity;
import com.ziyun.erp.modules.business.service.OrderScholarshipConfigService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 订单奖励金配置表
 *
 * @author jiangli
 * @email 
 * @date 2018-12-07 10:21:58
 */
@RestController
@RequestMapping("business/orderscholarshipconfig")
public class OrderScholarshipConfigController {
    @Autowired
    private OrderScholarshipConfigService orderScholarshipConfigService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("business:orderscholarshipconfig:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = orderScholarshipConfigService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("business:orderscholarshipconfig:info")
    public R info(@PathVariable("id") Integer id){
        OrderScholarshipConfigEntity orderScholarshipConfig = orderScholarshipConfigService.selectById(id);

        return R.ok().put("orderScholarshipConfig", orderScholarshipConfig);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("business:orderscholarshipconfig:save")
    public R save(@RequestBody OrderScholarshipConfigEntity orderScholarshipConfig){
        orderScholarshipConfigService.insert(orderScholarshipConfig);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("business:orderscholarshipconfig:update")
    public R update(@RequestBody OrderScholarshipConfigEntity orderScholarshipConfig){
        ValidatorUtils.validateEntity(orderScholarshipConfig);
        orderScholarshipConfigService.updateAllColumnById(orderScholarshipConfig);//全部更新
        
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("business:orderscholarshipconfig:delete")
    public R delete(@RequestBody Integer[] ids){
        orderScholarshipConfigService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

    /**
     * 修改随机奖励金倍数
     */
    @RequestMapping("/updateRandomHandler")
    public R updateRandomHandler(@RequestBody String dataValue){
        OrderScholarshipConfigEntity orderScholarshipConfigEntity = orderScholarshipConfigService.selectOne(new EntityWrapper<OrderScholarshipConfigEntity>().eq("config_type", "random"));
        orderScholarshipConfigEntity.setDataValue(dataValue);
        orderScholarshipConfigEntity.setUpdateName(UserUtils.getCurentUser().getUsername());
        orderScholarshipConfigEntity.setUpdateTime(new Date());
        orderScholarshipConfigService.updateById(orderScholarshipConfigEntity);
        return R.ok();
    }

    /**
     * 修改平均倍数
     */
    @RequestMapping("/updateAvgHandler/{avg}")
    public R updateAvgHandler(@PathVariable String avg){
        OrderScholarshipConfigEntity orderScholarshipConfigEntity = orderScholarshipConfigService.selectOne(new EntityWrapper<OrderScholarshipConfigEntity>().eq("config_type", "avg"));
        orderScholarshipConfigEntity.setDataValue(avg);
        orderScholarshipConfigEntity.setUpdateName(UserUtils.getCurentUser().getUsername());
        orderScholarshipConfigEntity.setUpdateTime(new Date());
        orderScholarshipConfigService.updateById(orderScholarshipConfigEntity);
        return R.ok();
    }

}
